package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {

    private StudentService service = new StudentServiceImpl();

    /**
     * 分页查询  展示数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        Page page = service.pageLimit(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        PageInfo info = new PageInfo(page);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        response.getWriter().write(json);


    }


    public void addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        ServletInputStream is = request.getInputStream();
        ObjectMapper mapper = new ObjectMapper();
        //次student是从前台通过json对象发送过来的
        Student student = mapper.readValue(is, Student.class);


        service.add(student);


    }


    //修改学生
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        ServletInputStream is = request.getInputStream();
        ObjectMapper mapper = new ObjectMapper();
        Student student = mapper.readValue(is, Student.class);
        System.out.println(student);

        System.out.println(student);

        service.update(student);


    }


    public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String number = request.getParameter("number");
        service.del(number);


    }




    private void dateConvert() {
        ConvertUtils.register(new Converter() {
            // 将参数value转换成Date对象并返回
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }







}
